package com.guigu.ssyx.acl.controller;


import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.ssyx.acl.service.AdminService;
import com.guigu.ssyx.acl.service.RoleService;
import com.guigu.ssyx.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/acl/user")
@Api(tags = "用户管理")
@CrossOrigin
public class AdminController {

    @Autowired
    AdminService adminService;

    @Autowired
    private RoleService roleService;

    //获取所有角色
    @ApiOperation("获取用户角色")
    @GetMapping("toAssign/{adminId}")
    public Result toAssign(@PathVariable Long adminId){
        //map集合返回两部分数据：所有角色和为用户分配的橘色
        Map<String, Object> map = roleService.getRoleByAadminId(adminId);
        return Result.ok(map);
    }

    @ApiOperation("用户角色分配")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestParam Long adminId,@RequestParam Long[] roleId){
        roleService.saveUserRoleRealtionShip(adminId,roleId);
        return Result.ok(null);
    }

    @GetMapping("{page}/{limit}")
    @ApiOperation("用户列表")
    public Result getList(@PathVariable Long page, @PathVariable Long limit, AdminQueryVo adminQueryVo){

        Page<Admin> adminPage = new Page<>(page,limit);

        IPage<Admin> pageModel = adminService.selectPage(adminPage, adminQueryVo);

        return Result.ok(pageModel);
    }

    @ApiOperation("获取用户")
    @GetMapping("get/{id}")
    public Result getUser(@PathVariable Long id){
        Admin admin = adminService.getById(id);
        return Result.ok(admin);
    }

    @ApiOperation("添加用户")
    @PostMapping("save")
    public Result save(@RequestBody Admin admin){
        adminService.saveOrUpdate(admin);
        return Result.ok(null);
    }

    @ApiOperation("修改用户")
    @PostMapping("update")
    public Result update(@RequestBody Admin admin){
        adminService.saveOrUpdate(admin);
        return Result.ok(null);
    }
    @ApiOperation("删除用户")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable String id){
        adminService.removeById(id);
        return Result.ok(null);
    }

    @ApiOperation("批量删除用户")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<String> idList){
        adminService.removeByIds(idList);
        return Result.ok(null);
    }




}
